package com.kkkk.rest.common.util;


import jakarta.validation.ConstraintViolation;
import jakarta.validation.ValidationException;
import jakarta.validation.Validator;
import org.springframework.stereotype.Component;

import java.util.Set;


/**
 * @author wyd10
 */

@Component
public class ValidatorUtils {

    private static Validator validator;


    public ValidatorUtils(Validator validator) {
        ValidatorUtils.validator = validator;
    }

    public static <T> T valid(T object) throws ValidationException {
        Set<ConstraintViolation<T>> violationSet = ValidatorUtils.validator.validate(object);
        for (ConstraintViolation<T> model : violationSet) {
            throw new ValidationException(model.getMessage());
        }
        return null;
    }


}
